package com.stone.design.patterns.observer.weatherdata;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Stone
 * @version V1.0
 * @description:
 * @date 4/18/18 22:31
 * @email redsstone@163.com
 */
public class WeatherData implements Subject{

    private List<Observer> mObservers;

    private float temperature;
    private float humifity;
    private float pressure;

    public WeatherData() {
        mObservers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        mObservers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {

        int i = mObservers.indexOf(observer);
        if (i > 0) {
            mObservers.remove(i);
        }

    }

    @Override
    public void notifyObservers() {
        for (int i = 0; i < mObservers.size(); i++) {
            Observer observer = mObservers.get(i);
            observer.update(temperature, humifity, pressure);
        }
    }



    public void setMeasurements(float temperature, float humifity, float pressure) {
        this.temperature = temperature;
        this.humifity = humifity;
        this.pressure = pressure;
        measurementsChanged();
    }


    public float getTemperature() {
        return temperature;
    }

    public float getHumifity() {
        return humifity;
    }

    public float getPressure() {
        return pressure;
    }

    public void measurementsChanged() {
        notifyObservers();
    }
}
